package tt.com.datamanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tt.com.common.model.ClassesTeam;
import tt.com.common.model.CremateStove;
import tt.com.common.util.PageUtil;
import tt.com.datamanager.mapper.ClassesTeamMappers;
import tt.com.datamanager.service.ClassesTeamService;

import java.util.Date;

@Service("classesTeamServiceImpls")
public class ClassesTeamServiceImpl extends ServiceImpl<ClassesTeamMappers, ClassesTeam> implements ClassesTeamService {
    @Autowired
    private ClassesTeamMappers classesTeamMappers;

    @Override
    public PageUtil selectAllClassesTeamByPage(PageUtil pageUtil) {
        Page<ClassesTeam> page = new Page<>(pageUtil.getPageIndex(), pageUtil.getPageSize());
        Page<ClassesTeam> classesTeamPage = baseMapper.selectPage(page, null);
        // 设置总条数 自动计算总页数
        pageUtil.setTotalSize((int) classesTeamPage.getTotal());
        pageUtil.setData(classesTeamPage);
        return pageUtil;
    }

    @Override
    public Integer updateClassesTeam(ClassesTeam classesTeam, String updateUserId) {
        // 设置修改时间
        classesTeam.setLastUpdateTime(new Date());
        // 设置修改操作人id
        classesTeam.setLastUpdateUserId(updateUserId);
        return baseMapper.updateById(classesTeam);
    }

    @Override
    public Integer deleteClassesTeam(Long id, String deleteUserId) {
        //根据id获取排班信息
        ClassesTeam classesTeam = baseMapper.selectById(id);
        if (classesTeam == null){
            //若数据不存在，返回-1
            return -1;
        }
        if (classesTeam.getDataStatus() == 1){
            //判断是否已经被删除过
            return -2;
        }
        //设置删除时间
        classesTeam.setDeleteTime(new Date());
        //设置删除操作人id
        classesTeam.setDeleteUserId(deleteUserId);
        classesTeam.setDataStatus(1);
        baseMapper.updateById(classesTeam);
        return baseMapper.deleteById(classesTeam.getId());
    }

    @Override
    public Integer insertClassesTeam(ClassesTeam classesTeam) {
        QueryWrapper<ClassesTeam> wrapper = new QueryWrapper<>();
        wrapper.eq("id", classesTeam.getId());
        if (baseMapper.selectOne(wrapper) != null) {
            // 重复添加返回-1
            return -1;
        }
        // 设置创建时间
        classesTeam.setCreateTime(new Date());
        classesTeam.setDataStatus(0);
        return baseMapper.insert(classesTeam);

    }
}
